package v2;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.scheduler.PriorityScheduler;
import us.codecraft.webmagic.selector.Selectable;

public class SnygPageProcessor extends ShangCheng implements PageProcessor{
	
	private Site site = Site.me().setCharset("utf-8")
			.setUserAgent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36")
			.setSleepTime(1000)
			.setRetryTimes(3);

	private String entryUrl;
	
	public String getEntryUrl() {
		return entryUrl;
	}

	public void setEntryUrl(String entryUrl) {
		this.entryUrl = entryUrl;
	}

	@Override
	public void process(Page page) {
		if(page.getUrl().regex("http://list.suning.com.*").toString() != null){
			//获取商品搜索页中各分页的url并添加到page中,如果pageTotal不为0则跳过
			if(pageTotal == 0){
				List<Selectable> selectables = page.getHtml().xpath("//div[@id='bottom_pager']//a[@pagenum]").nodes();
				if(selectables != null && selectables.size()>0){
					pageTotal = Integer.valueOf(selectables.get(selectables.size()-1).xpath("/a/html()").toString());
				}
				String[] urlSplit = page.getUrl().toString().split("-");
				
				for(int i=1;i<pageTotal;i++){
					if(urlSplit != null && urlSplit.length>3){
						urlSplit[2] = String.valueOf(i);
					}
					Request request = new Request();
					String url = "";
					for(String urlStr : urlSplit){
						url += urlStr+"-";
					}
					url = url.substring(0,url.length()-1);
					request.setUrl(url);
					page.addTargetRequest(request);
				}
			}
			//搜索获取商品列表
			searchGdsInfo(page);
		}else if(page.getUrl().regex("http://product\\.suning\\.com/.*").toString() != null){
			//获取商品下的所有skuId
			genSkus(page);
		}else if(page.getUrl().toString().contains("getAllPriceFourPage")){
			//获取sku的相关信息
			genSkuInfo(page);
		}
	}
	/**
	 * 搜索获取商品列表
	 * @param page
	 */
	private void searchGdsInfo(Page page){
		Selectable xpath = page.getHtml().xpath("//div[@class='border-out']");
		List<Selectable> nodes = xpath.nodes();
		for(Selectable s : nodes){
			String gdsUrl = s.xpath("//p[@class='sell-point']//a/@href").toString();
			Request request = new Request(gdsUrl).setPriority(0);
			page.setSkip(true);
			page.addTargetRequest(request);
		}
	}
	
	/**
	 * 获取商品下的所有skuId
	 * @param page
	 */
	private void genSkus(Page page){
		//skuid
		String skuid = page.getUrl().replace("http://product.suning.com/", "")
				.replace(".html", "").toString();
		
		String linesStr = page.getHtml().xpath("//script/html()").regex(".*\"clusterMap\":.*").toString();
		if(StringUtils.isNotBlank(linesStr)){
			String[] lines = linesStr.split("\n");
			String gdsInfoJson = "";
			for(String line : lines){
				if(line.contains("\"clusterMap\":")){
					gdsInfoJson = line.trim();
					gdsInfoJson = "{"+gdsInfoJson.substring(0, gdsInfoJson.length()-1)+"}";
				}
			}
			//获取json串中所有的SkuId，并拼接成url
			JSONObject jsonObject = JSONObject.parseObject(gdsInfoJson);
			JSONArray jsonArray = JSONArray.parseArray(jsonObject.get("clusterMap").toString());
			for(int i=0;i<jsonArray.size();i++){
				JSONObject paramObject = JSONObject.parseObject(jsonArray.get(i).toString());
				JSONArray jsonArray2 = JSONArray.parseArray(paramObject.get("itemCuPartNumber").toString());
				for(int j=0;j<jsonArray2.size();j++){
					JSONObject jsonObject2 = jsonArray2.getJSONObject(j);
					String skuId = Integer.valueOf(jsonObject2.get("partNumber").toString()).toString();
					Request request = new Request("http://product.suning.com/"+skuId+".html").setPriority(1);
					page.addTargetRequest(request);
				}
			}
		}
		String title = page.getHtml().xpath("//title/html()").toString();
		String seller = page.getHtml().xpath("//span[@class='zy']/html()").toString();
		List<Selectable> nodes = page.getHtml().xpath("//table[@id='itemParameter']/tbody/tr").nodes();
		StringBuilder sb = new StringBuilder();
		for(Selectable s : nodes){
			String key = s.xpath("//td[1]//span//html()").get();
			String value = s.xpath("//td[2]/allText()").get();
			if (key == null || value == null){
				continue;
			}
			sb.append(key.trim() + ":" + value.trim().replace(";", "_").replace(":", "_") + ";");
		}
		
		Request request = new Request();
		request.putExtra("skuId", skuid);
		request.putExtra("title", title).setPriority(2);
		request.putExtra("seller", seller);
		request.putExtra("detail", sb.toString());
		
		//拼接获取价格的地址
		String id = "";
		if(skuid.contains("-") && skuid.split("-").length>2){
			id = skuid.split("-")[0]+"_";
		}else if(skuid.contains("_") && skuid.contains("detail")){
			id = skuid.split("_")[2]+"_"+skuid.split("_")[1];
		}else if(skuid.contains("/")){
			id = skuid.split("/")[1]+"_"; 
		}else{
			id = skuid+"_";
		}
		
		request.putExtra("id", id);
		
		request.setUrl("http://icps.suning.com/icps-web/getAllPriceFourPage/000000000"+id+"_025_0250101_1_pc_showSaleStatus.vhtm?callback=showSaleStatus");
		page.addTargetRequest(request);
		page.setSkip(true);
	}
	
	/**
	 * 获取sku的相关信息
	 * @param page
	 */
	private void genSkuInfo(Page page){
		String aaString = page.getJson().toString();
		aaString = aaString.substring("showSaleStatus(".length(), aaString.length()-2);
		JSONObject object = JSONObject.parseObject(aaString);
		JSONArray array = object.getJSONArray("saleInfo");
		String refPrice = array.getJSONObject(0).getString("refPrice");
		String netPrice = array.getJSONObject(0).getString("netPrice");
		String promotionPrice = array.getJSONObject(0).getString("promotionPrice");
		page.putField("skuId", page.getRequest().getExtra("skuId"));
		page.putField("title", page.getRequest().getExtra("title"));
		page.putField("seller", page.getRequest().getExtra("seller"));
		page.putField("detail", page.getRequest().getExtra("detail"));
		page.putField("refPrice", refPrice);
		page.putField("netPrice", netPrice);
		page.putField("promotionPrice", promotionPrice);
	}

	@Override
	public Site getSite() {
		return site;
	}

	public static void main(String[] args) throws Exception{
		String url = "http://list.suning.com/0-20006-0-0-0-0-0-0-0-0-12256.html";
		long startTime = new Date().getTime();
		SnygPageProcessor snygPageProcessor = new SnygPageProcessor();
		snygPageProcessor.setEntryUrl(url);
		Spider spider = Spider.create(snygPageProcessor).scheduler(new PriorityScheduler());
		spider.addUrl(url);
		spider.thread(3).run();
		
		long endTime = new Date().getTime();
		System.out.println("时间："+String.valueOf((endTime-startTime)));
	}
}
